<template>
	<view class="wrap">
		<cu-custom :isBack="true" textColor="#000" fhColor="#000" xbgColor='#FAFAFA'>
			<block slot="content">搜索</block>
		</cu-custom>
		<view class="Search">
			<!-- 搜索 -->
			<view class="SearchInputBox" :style="{top:CustomBar+'px'}">
				<image :src="imgUrl + '/static/search.png'" alt="" class="SearchIcon">
					<input type="text" class="SearchInput" v-model.trim="keyWordsa" placeholder="搜索"
						placeholder-class="SearchInputYs" @input="keyWordsChange">
					<view class="btn" @click="getCommodityList">搜索</view>
			</view>
			<!-- 历史记录 -->
			<view class="history" v-if="keyWordsa.length == 0" :style="{top:CustomBar+'px'}">
				<view class="historyTitle">
					<view class="l">历史记录</view>
					<image class="r" :src="imgUrl+'/static/closeLsjl.jpg'" @click="closeLsjl" mode="heightFix"></image>
				</view>
				<view class="historyList">
					<view class="historyListItem" @click="setKeywords(item)" v-for="(item,index) in HistorySearch"
						:key="index">
						{{item}}
					</view>
				</view>
			</view>
			<view class="container" v-else>
				<view class="classification" :style="{top:CustomBar+'px'}">
					<view class="classificationItem default" :class="{
				  Activeclassification: CurrentClassification == 'Currentdefault',
				}" @click="ChangeCurrentClassification('Currentdefault')">
						默认
					</view>

					<view class="classificationItem SalesVolume" :class="{
				  Activeclassification: CurrentClassification == 'CurrentSalesVolume',
				}" @click="ChangeCurrentClassification('CurrentSalesVolume')">
						<view class=""> 销量 </view>
						<!-- <image src="" alt="" class="classificationicon"></image> -->
					</view>

					<view class="classificationItem price" :class="{
				  Activeclassification: CurrentClassification == 'Currentprice',
				}" @click="ChangeCurrentClassification('Currentprice')">
						<view class=""> 价格 </view>
						<!-- <image src="" alt="" class="classificationicon"></image> -->
					</view>

					<view class="classificationItem screening" :class="{
				  Activeclassification: CurrentClassification == 'Currentscreening',
				}" @click="ChangeCurrentClassification('Currentscreening')">
						<view class="screeningtext"> 筛选 </view>
						<img class="screeningIcon" :src="imgUrl + gameDetailsData.Classification.DropArrow" />
					</view>
				</view>
				<scroll-view :scroll-y="true" class="CommodityListContentBox" @scrolltolower="scrolltolower"
					:style="{height:height-CustomBar+'px',top:CustomBar+'px'}">
					<view class="list">
						<shopping-list v-for="(item,index) in commodityList" :key="index" :listItem="item"></shopping-list>
					</view>
				</scroll-view>
			</view>
		</view>
	</view>
</template>

<script>
	import gameDetails from "@/static/lib/public.js";
	import shoppingList from '@/components/shopping/shoppingList.vue'
	export default {
		data() {
			return {
				CustomBar: this.CustomBar,
				gameDetailsData: gameDetails,
				imgUrl: this.$http.imgUrl,
				height: '',

				// 搜索值
				keyWordsa: '',
				keyWords: '',

				// 历史记录列表
				HistorySearch: [],
				// 商品详情
				commodityList: [],
				pageNumber: 1,
				totalPage: '',

				CurrentClassification: 'Currentdefault',


				ScreeningCondition: {
					pageNumber: 1,
					pageSize: 10,
					goodsCategoryId: 0,
					field: "",
					highPrice: "",
					keyWords: "",
					lowerPrice: "",
					orderBy: "",
					totalPage: "",
				},
			};
		},
		components:{
			shoppingList
		},
		created() {
			this.height = uni.getSystemInfoSync().windowHeight
			this.HistorySearch = uni.getStorageSync('historyList') ? JSON.parse(uni.getStorageSync('historyList')) : []
		},
		methods: {
			// 搜索商品
			async commodityListData() {
				const url = this.api.home.GoodsList
				const res = await this.https.get(url, {
					keyWords: this.keyWords,
					pageNumber: this.pageNumber,
					pageSize: 15,
					token: uni.getStorageSync('token')
				})
				this.commodityList = this.commodityList.concat(res.data)
				this.totalPage = res.totalPage
				console.log(res, '商品列表');
			},

			// 搜索
			getCommodityList() {
				if (!this.keyWordsa) {
					uni.showToast({
						title: '搜索内容不可为空',
						icon: "none"
					})
					return
				}
				this.keyWords = this.keyWordsa
				this.HistorySearch.unshift(this.keyWordsa)
				this.lsjlQc()
				this.initCsh()
			},

			// 选择记录
			setKeywords(value) {
				this.keyWordsa = value
				this.keyWords = value
				this.initCsh()
			},

			// 改变输入框
			keyWordsChange() {
				if (!this.keyWordsa) {
					this.keyWords = ''
				}
			},

			// 历史记录去重
			lsjlQc() {
				this.HistorySearch = [...new Set(this.HistorySearch)]
				uni.setStorageSync('historyList', JSON.stringify(this.HistorySearch))
			},

			// 初始化
			initCsh() {
				this.pageNumber = 1
				this.commodityList = []
				this.commodityListData()
			},

			// 触底
			scrolltolower(e) {
				if (this.pageNumber >= this.totalPage) {
					uni.showToast({
						title: "暂无数据",
						icon: "none",
					});
					return;
				}
				this.pageNumber++;
				this.commodityListData();
			},

			ChangeCurrentClassification(condition) {
				if (condition == "Currentprice") {
					this.SalesVolumeSrc = "0";
					if (this.PriceSrc === "0") {
						this.ScreeningCondition.field = "goods_price";
						this.ScreeningCondition.orderBy = "desc";
						this.CommodityList = [];
						this.getCommodityList();
						this.PriceSrc = "1";
					} else {
						this.PriceSrc = this.PriceSrc === "1" ? "2" : "1";
						this.ScreeningCondition.orderBy =
							this.PriceSrc === "1" ? "desc" : "asc";
						this.CommodityList = [];
						this.getCommodityList();
					}
				} else if (condition == "CurrentSalesVolume") {
					this.PriceSrc = "0";
					if (this.SalesVolumeSrc === "0") {
						this.SalesVolumeSrc = "1";
						this.ScreeningCondition.field = "goods_sales";
						this.ScreeningCondition.orderBy = "desc";
						this.CommodityList = [];
						this.getCommodityList();
					} else {
						this.SalesVolumeSrc = this.SalesVolumeSrc === "1" ? "2" : "1";
						this.ScreeningCondition.orderBy =
							this.SalesVolumeSrc === "1" ? "desc" : "asc";
						this.CommodityList = [];
						this.getCommodityList();
					}
				} else if (condition == "Currentscreening") {
					this.SalesVolumeSrc = "0";
					this.PriceSrc = "0";
					this.modalName = "bottomModala";
				} else if (condition == "Currentdefault") {
					this.PriceSrc = "0";
					this.SalesVolumeSrc = "0";
					this.CommodityList = [];
					this.getCommodityList();
				}
				this.CurrentClassification = condition;
			},

			// 清空历史记录
			closeLsjl() {
				uni.clearStorageSync('historyList')
				this.HistorySearch = ''
			},

			// 跳转详情
			toShopDetails(id) {
				uni.navigateTo({
					url: '/pagesA/HomePage/ShopDetails?id=' + id
				});
			},
		}
	}
</script>

<style lang="scss" scoped>
	.wrap {
		height: 100vh;
		background-color: #fafafa
	}

	.SearchInputBox {
		width: 702rpx;
		height: 72rpx;
		background: rgba(153, 153, 153, 0.12);
		border-radius: 36rpx;
		display: flex;
		align-items: center;
		margin: 0 auto;
		padding: 0 10rpx 0 20rpx;
		position: fixed;
		left: 50%;
		transform: translateX(-50%);
		z-index: 2;

		.SearchIcon {
			margin-top: 4rpx;
			width: 44rpx;
			height: 44rpx;
		}

		.SearchInput {
			height: 100%;
			margin-left: 20rpx;
			flex: 1;
			border-left: 1rpx solid rgba(153, 153, 153, 0.2);
			height: 40rpx;
			line-height: 40rpx;
			padding: 0 20rpx;
			font-size: 28rpx;
			font-family: PingFangSC-Regular, PingFang SC;
			font-weight: 400;
			color: #666666;
		}

		.SearchInputYs {
			font-size: 28rpx;
			font-weight: 400;
			color: #999999;
		}

		.btn {
			width: 120rpx;
			height: 56rpx;
			background: #4D8BFF;
			border-radius: 28rpx;
			font-size: 28rpx;
			font-weight: 500;
			color: #FFFFFF;
			line-height: 56rpx;
			text-align: center;
		}
	}

	.history {
		width: 702rpx;
		margin: 0 auto;
		padding-top: 40rpx;
		position: fixed;
		left: 50%;
		transform: translate(-50%);
		margin-top: 72rpx;
		z-index: 2;

		.historyTitle {
			margin-bottom: 24rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;

			.l {
				font-size: 28rpx;
				font-weight: 500;
				color: #666666;
			}

			.r {
				width: 20rpx;
				height: 23rpx;
			}
		}

		.historyList {
			display: flex;
			flex-wrap: wrap;

			.historyListItem {
				height: 60rpx;
				background: #FFFFFF;
				border-radius: 40rpx;
				border: 2rpx solid rgba(153, 153, 153, 0.2);
				padding: 0 40rpx;
				margin-right: 16rpx;
				line-height: 56rpx;
				margin-bottom: 16rpx;
				font-size: 28rpx;
				font-weight: 400;
				color: #666666;
			}
		}
	}

	.container {
		width: 702rpx;
		margin: 0 auto;

		.classification {
			width: 100%;
			display: flex;
			justify-content: space-between;
			position: fixed;
			left: 50%;
			transform: translateX(-50%);
			margin-top: 72rpx;
			z-index: 2;

			.classificationItem {
				width: 25% !important;
				text-align: center;
				height: 100rpx;
				line-height: 100rpx;

				.classificationicon {
					width: 30rpx !important;
					height: 30rpx !important;
				}
			}

			.price,
			.SalesVolume {
				display: flex;
				align-items: center;
				justify-content: center;
			}

			.screening {
				display: flex;
				align-items: center;
				justify-content: center;

				.screeningIcon {
					width: 16rpx;
					height: 16rpx;
					margin-left: 10rpx;
				}
			}

			.Activeclassification {
				//border-bottom: 3rpx solid #376af6;
				color: #376af6;
			}
		}

		.CommodityListContentBox {
			position: fixed;
			padding-top: 172rpx;
			width: 702rpx;

			.list {
				width: 702rpx;
				// margin: 32rpx 24rpx 0 24rpx;
				margin-bottom: 24rpx;
				display: flex;
				justify-content: space-between;
				flex-wrap: wrap;

			}
		}
	}
</style>